{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 化简 \n",
    "\n",
    "### 化简\n",
    "\n",
    "现在开始学习一些有趣的数学。 符号操纵系统最有用的功能之一就是能够简化数学表达式。 SymPy具有数十种功能来执行各种简化。 还有一个称为simple()的通用函数，它试图以一种智能的方式应用所有这些函数，以得出最简单的表达式形式。 这里有些例子\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAA0AAAASCAYAAACAa1QyAAAABHNCSVQICAgIfAhkiAAAAHZJREFU\nKJFjYKACCGFgYJjMwMBwmIGB4RMDA8N/BgaGJYQ0XYAq/MzAwHCdWE2ODAwMqgwMDIwMDAwOuDSx\noPH3EzKVgYGBgYkYRaOaBlwTeuQGQDEDAwODBJS2ZGBgWABlv2FgYChBN6SBAZJ0cOEH5LiMzgAA\n6XoX52TB9a4AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$1$$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "x, y, z = symbols('x y z')\n",
    "init_printing(use_unicode=True)\n",
    "simplify(sin(x)**2 + cos(x)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAADIAAAASCAYAAAAZk42HAAAABHNCSVQICAgIfAhkiAAAAU1JREFU\nSInt1b8rRWEcx/GXHxnsBmVDmdx/gDrKZJIsNmX1F5hsDEwGZbqDxWaUxSClLEoxiO6EMhCFiGs4\nj7pdXOe53UPiXafO832ez/f5fM95fvDPtzCOJWzjBmWs/qijOtmXmr/FkV9cyBB60YREjUKaq9qb\nYfBYVbwJxdA33zifX7KF4zBvFAU84xAtFfHFkGylEe7qJBG5tIpBMBnaM6G95v0f/E4SkYV04R4l\nTAfxBtoa7y2KRB2bfS6IythBe0ZdqUKX5YkxldTStH4iuqx4n8JdxslO8JDVGc4ixkYzgRecS7/A\ncp6TRZCI+IsjeMQBOqSX0BP6cjIXQyJjIQPSJXSKzhAbD+L1nMzFkMhQSAHX0uXUXdW3FxIM5mDu\nK0al10FRenKWpfvwLbZQObgHF7hC/wfJhkOC3Xy81mRW7VOv9AOe/vlbvAJAOGPWFb/OwQAAAABJ\nRU5ErkJggg==\n",
      "text/latex": [
       "$$x - 1$$"
      ],
      "text/plain": [
       "x - 1"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify((x**3 + x**2 -x -1)/(x**2 + 2*x + 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAIIAAAAVCAYAAACKYDYGAAAABHNCSVQICAgIfAhkiAAAA2lJREFU\naIHt2U9oXFUUBvBfa1FjhYL/UNGVrejGIoogKIxWKAiCaDeurNRFwSoiiBgQu2uRikgXggsJooiQ\n0oII0pVgBf8UFQpmYyUVNKWCf8GojR0X5047ed55c6edSV/ifDAk755z7nzfl3Pfu/eFMcaowZs4\ngbXnm8gQcRva2FaQuxL1M5gHbscpPDNKRkPA5Xgc+/EN5vErDgmhqzM1+zGHS2vmXS76YQv24iP8\nJv7Ib/WpKfEAHMQvmDg3jiPHdiH8B7yNXXhDcG9jGqsqNXek2GTNvMtFP3wl9PyOGWWNUOKBG8Vq\neP3cOY4c9+IB/135V+M7IfbhTN0MjuGCTGw56Yd7sEE0fEtZI1DvAdidJtuUiR1MsYcq46swlWK7\nC0gsBSYFn72Z2IsptjkTq9NPsz1oKW+EOg/AYSzIb5I24h98bXEnvZwmbdIqelZweiUTuy/F9mRi\ndfpptgct5Y1Q54G1woQjNRNMpQm2puvOyntXfnN2PrBGaOjV8etS7LPKeIl+mutBS3kj9PIA8Xxs\ni9tfL1wnduez2JHyP8CFpWyXAHsEr/drcuZxvDJWop/metBS3gjkPQB3OtPZddiV8tr4GJcUfvFs\nV13Jp1RQN55KtTO4rCbve7H6u1Gqn2Z60BqwZpEHa7oC8+nnxX0m+LHr9234o/CLj+LPwlziWDgI\nnsCr4vm9CT/V5E44o7eDUv0014NBkPMAXCs66lBN8SPieDWXcl8bNruzxNOCzxFc1Sd3tdBwtDJe\nop/metBSfkfo5QHiCHTC4m7vxv34W5h9pbj9nsRNA9EdPp4TBnyJKwryb075+yrj/fTTXA8YrBF6\neXAa0ylhfWX8LnH7+xbXpLEtKffAQHSHixcSh8Pq9wTdeCzV7MjEeumnuR500FLeCHUeIG57bfG8\n7WCjeOU6hxsq+Z+n/LuL6Q4Pj6bvXhDvC3ZmPlszde+kmuszsZx+muvBg+I4OyVOLm1xu++MZd8T\nqPcAcQQ6jk/T9fp0/TNuyeR3Xkx8Mgj7IWGn/rvuDys168QGqdcKrupneXswm6np58FpPJ8muXUo\nVJuFJ/VfvStZP2UeII5Px/DeqBktMSbEcWy6T95K1U+NB7n/Pi3gC1wkNmEnR0pt6bABf+El8bzv\nhZWqn3IPxhhjjP81/gWO9lBHKMQ+5gAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$$\\left(x - 2\\right) \\left(x - 1\\right)$$"
      ],
      "text/plain": [
       "(x - 2)⋅(x - 1)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(gamma(x)/gamma(x-2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是simplify()是有陷阱的。它只能应用所有SymPy中的简化方法，并使用启发式算法来发现最简单的结果。但是'简单'是非常难以定义的。例如, 我们需要简化`x*x + 2*x + 1`为`(x+1)**2`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAG4AAA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latex": [
       "$$x^{2} + 2 x + 1$$"
      ],
      "text/plain": [
       " 2          \n",
       "x  + 2⋅x + 1"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simplify(x**2 + 2*x + 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "并未能得到我们期望的。这里有个函数能够实现这样的简化，叫做`factor()`，我们将在下面的章节讨论。\n",
    "\n",
    "另一个`simplify()`的陷阱在于它会具有不必要的缓慢，在它找到最简之前会尝试许多简化的可能。如果你已经清楚的知道什么类型的简化是你需要的，最好是应用特定的简化函数来执行这些简化。\n",
    "\n",
    "应用特定的简化函数相对于`simplify()`，具有以下优点: 特定函数对它们的输出形式具有一定的保证。 这些将在下面的每个功能中讨论。 例如，当`factor()`在有理系数的多项式上调用时，可以保证将多项式分解为不可约因子。 `simplify()`没有保证。 这完全是一种试探，就像我们在上面看到的那样，它甚至可能错过了SymPy能够进行的可能的简化类型。\n",
    "\n",
    "当您只想将表达式缩减为更简单的形式时，当以交互方式使用时，`simplify()`最好。 然后，一旦看到`simplify()`返回什么，就可以选择应用特定功能，以获得更精确的结果。 当您不知道表达式将采用哪种形式并且需要使用catchall函数来简化它时，它也很有用。\n",
    "\n",
    "### 多项式/有理数 函数简化\n",
    "\n",
    "#### expand\n",
    "\n",
    "`expand()`是SymPy中最常用的简化函数之一。尽管它包含了许多的范围，现在，我们会研究这样的函数来展开多项式表达式，例如: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAG4AAAAWCAYAAAAhKqlXAAAABHNCSVQICAgIfAhkiAAAAtJJREFU\naIHt2E/IVGUUx/GPJS3CIOwPCa6kIKG0bWAw+QdBiKLcuOoJWwRFuFBCIXhpo4vyDyJGC3mDWhSB\nQgnmogQVggSFF3RhyNBCDSWtoCApXZznpXGcmfveO3dmrO4XLsx97nPOc3733HvumYeG/yVb8T1+\nxRV8iacmGlHDnPgar4lkPY2DuIyFkwyqoTwL8BdemHQg/3XuqdnfA9nntZr9FvEQXhdv/A/4A7/g\nBDaqX+eoWI+9OC4+PzfxyTgW/hynce84FuvgDSHyIj7FdhzA9Tz+BeaNOaYqnBHx/oZzxpS4neLG\nLalon0SgrQq2K0V57n6zHsOP2e8rFeMqQ1JdAzyPJ8RD1jIgcd1Cj+bJL3eNz8N0vrajh59d2CBu\n4IVqMQ/FN6Kj/btr/DI+zL9bXdeqah0l3+J8XrsUy0Vzcdbt5e6D7OyjHjZ78BOWlg7zdpLhntZ+\nbMl+d3WNV9FaRFKfhpaSpXI6G6R8vi2ff+bON3Sf+IiuFGVp9lhQIdCk/sTNx0z2u7bH9Wlz1zoX\nkgkmbrHoytp4KxsfwX095t7sc0xVCDSpP3HvZ5+H+1wvo3UuJBNMHNGVzSbhJO6vIZBO2vonvdcx\nXWGNt7PtOYM3BKpqbRfEPKyGlgGJm9/H6ErH7434veSiRezGg11jz+BFfCxuSidnSvp/U3x7z2IV\nfh4wt6rWUWsozQbRnV0SGd8/6gUzST1lZlP2M4NHC+bWrTWZUKlchz+F6EdEmbmBJ2sIpIhkeNHv\nZB+n8XDB3FFoTSaQuBWiTFzAojy2PhsfqiGQIpLhRL+b7U8p3uQeldZkTImb/cYtx1dif2+NKB3E\nVtEpUbefE3todyOv4j3xv+y4aEy6aYsG4W7W+lI+iL9V8Kx/Gpur2Dw7+XGxw3ANy3o4Wy0y/90I\nAu0kqf60Tinu6o4ZvdZkuDduymAN7Yp+GxoaGhoa/jXcAuCLBYCZODogAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$x^{2} + 2 x + 1$$"
      ],
      "text/plain": [
       " 2          \n",
       "x  + 2⋅x + 1"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+1)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'x^{2} + 2 x + 1'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(expand((x+1)**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGIAAAAWCAYAAAA7FknZAAAABHNCSVQICAgIfAhkiAAAAmZJREFU\naIHt2E2ITWEcx/GPMRRREqJsvCzIy+xEmY2XDdnIFDuarZKVlyQLRV5TpCw0xUYSUl5mNihRs0Kx\nkDFFhibNpBgvM67F89B13cs9957bvZf7rdM95/yf/+//O/ec53nOc2jwT7AL3XiPflzDwqo6+k+5\nhS3Cn78Il/EGk6tpqgETMIJ11TZSbzSlrDcxag6krFvPtOIS+vA5/nZiTXaj0SkXPYuv2ItMytr1\nyB6cw1RcF27AK8yK8a5KFD2G15hdCfE6pE14GLuEkSKXMX9K7ozJ63POj0JHjB3Mk3dc6HLzknlN\nlVK9V4Im9OCD0BsS0yJMtk/8OmwdFS7kTJ6cE3iL+aUUTJFSvFeK5bHmRTRjLXZgG5YVK9IRRTbH\n493x+ILfJ/dTwhpiBaZnbRNK8182HYr3Xkm2x7on8SjuZ293FNFTZmIIvdgaE29ibJ62uQV+bPvK\nuYoySOK9khyItYfxDCuFh3NB9JPB7SRCGdzD+JSN9ip8E/Nt5xNol+I9bT+HYrsRYcjMZhxexvjP\nYaq5gFB/1n47Pv7tShLyHJ8StH+doG0p3tP282Md1YOHObEh4YtEO5bgfiGRTfgmvAVlcDqBwWpT\nK97Xx/rdBeKHY3xnIYE1+ILHwmTyVFigVfO1tFhqyfuUWHtQ/vnphnAjNuZLXi504x7MiOc2xIQr\naTtNmVr0fj7W359zfrXQawcxKTepJQb6MCcn1h0FW9N2mhK16n2a8MaUwV0cEdYVw0JvactNmCt8\nuh7A4jyCq6LYg8r4LYta9z5Z+PTzQhg23+EqllbJT4MGDRo0qEO+A7YizfgC6DAWAAAAAElFTkSu\nQmCC\n",
      "text/latex": [
       "$$x^{2} - x - 6$$"
      ],
      "text/plain": [
       " 2        \n",
       "x  - x - 6"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+2)*(x-3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "给定的多项式, `expand()`将把它变成单项式和的规范形式。\n",
    "\n",
    "`expand()`听起来不像是一个简化函数。 毕竟，顾名思义，它使表达式更大而不是更小。 通常是这种情况，但是由于取消，表达式在调用`expand()`时通常会变小。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAB4AAAASCAYAAABM8m7ZAAAABHNCSVQICAgIfAhkiAAAAOlJREFU\nSInt1LFKA0EURuFPyROoiM8TCysrH8AiFoIQ0gmCYGdjE+zFQruAlb1FykCKgDYKwSKlmEpBJSn2\nirLKbpTdLcQDw8CdnzncGWb4AyxiCxe4xRPG6KKB+bLE25hghHMc4gSPUe9grgxxHeu+draC+5Bv\nlCHOYi/Ex++F0s49xUvMrxX5QA0DScdrVYqPQnqZFRpGaNZxliNtRu4GC58XaqngHZ5ze/hglLG2\ngzausYqHH+z7a1qSTgdYrkIIuyHtY6kq6X5Ie1J3mqbIL2wTp3iTfBTjbzLDyBTKgfxXcFW09J9c\npkjOQNujE1axAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$-2$$"
      ],
      "text/plain": [
       "-2"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((x+1)*(x-2)-(x-1)*x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### factor\n",
    "\n",
    "`factor()`接受多项式并将其分解为有理数上的不可约因子。 例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAI0AAAAYCAYAAADH9X5VAAAABHNCSVQICAgIfAhkiAAAA5tJREFU\naIHt2UuIHFUUxvFfosSMChFxoaIuNA8FNYoiChHaEFwIooib2bUmO3WTXQLC7BTxiYjgahRBI4EI\n2ehsAj5AjWhAfIBERjBONAtFwSiOjotb01NTVlfd6q7qbpr6Q9Fdt06dOt/Xt27dW01LS8tEcQDH\n8RvO4CiuH2tFLRPPu3hI6Cg34AhO4+JxFtUUr+FnXDDuQmrkFqxgb0RsU/ovxD+4t+a8dVPFK3Ar\n/sX+piqqkQfxIt4XHgEreL0g/giWhB+vH03qv0yocVcDuctowqseC/gVM8PVOBJOCOJ/x9fKjbgt\niTlYENOk/rfwOc5pIHcZTXgFtgt32SvD1zgS7sI2bEBHuREEw76X/8M1qf9Z/IirBzy/K+jrDHh+\nbV5tzAQ9nCQ9lJNgIbnQA5n2DZhPjj1ZVnnNHMO3ybVjeRNXYU/OsSL9DO7Bc5jFbnxXodY6qdur\nHp9iWf4EcKcwifvK+p73TFLIuEenjri7Z08S93TOsSL9DObBC/gJ15XUVUbXcCNNmo7hvUIwahlf\nFCSZT5J0k/2Dyf4h/x+1Rk1HnBFbkrhPMu0x+qnmwUvCpHM3Lk1tUZPLDF2j7zT9vOqxPQlYKEhy\nBc5iEY8m8e9gU2y1DdIRZwRBw+lMW4x+qnmw0mebi6gxS9foOw35XvW4w9odU8QT1sR/iPNjqhRM\n7mdi3hYjKE2nwnmnhFElTax+BvcglkXVvJqvmL9jCK/OTX0/m3xuLklyJvV9L/6IuDCcxJ+RsYSV\nRlPMWNO7Sqx+BvcgludxUabtJtyHV4VOleZEzddPk+dVj8uF3vdBQYJZYUm6lMS+XGd1Q9IRd/ds\nFDSczLTH6Gd8HnSN/vGU61V64rYk3EE7+iS4R+jlX+JGfIN9uLZqxWNmh7BEzt6dZfqZHg9iyfUq\n3WlW8B4uwdbMybtwGD/gbsHcx4XH26jfzQzL7cnnsUx7kX6my4NY+nm1jlnBvEdSbTuF1+pLuCYT\nfzyJv7OeGitzvzAJnBdWMCvCULralvd+4Q1hYndlzrE8/UyGB13DPZ7q9qrHJmF59XGyvzXZ/0UY\njrOsvvz5qEr1NTKneFWxmInfIkzq3u6TL6ufyfGga7hOM6der9ZxIEly84DFTTKPKR8Vpll/FWK8\n6rFZ+JPqaJMVjYEZYRl/uCRuWvVXodCrvH96l/EZzhP+i/m7sdJGyzb8haeE+Uk/plV/FWK9amlp\naWlpmRT+A6NDXlOYCUKdAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$\\left(x - 1\\right) \\left(x^{2} + 1\\right)$$"
      ],
      "text/plain": [
       "        ⎛ 2    ⎞\n",
       "(x - 1)⋅⎝x  + 1⎠"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**3 - x**2 + x -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAGEAAAAaCAYAAACn4zKhAAAABHNCSVQICAgIfAhkiAAABCtJREFU\naIHt2WuIVVUUB/DfqIk2gb0pCQkRS+hJ79AIs4IghCyoPuQVQ4oeZBKlEFlEKkklmlFCWZBQTFlZ\nUFaU6dCTDKzsg4UVZSZkFiWlNX1Y+zK34z3nPscw7x8uZ87a63X2XnuttffQQQf7CWbjQ/yCbViF\nE/5Tj/ZDvIZpYuJPxEr8gEPbofwp/Ijudijbh3Ea+jC9Tv6D8BcubdXw6fgbt7aqaIBxGK4V0bcJ\nO7ED68SkDWqTnZXYIia4Fo4Wiza+VaOr8TOGt6pogHGd+ODv8TTm4XHhex960NUGO2cmfXPq4H0W\n6zG4FYNjxS54rBUlDaIkPvL8BuUmim2fjfij8E3SO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uiggw46aC/+AWi/JlvLmENgAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$z \\left(x + 2 y\\right)^{2}$$"
      ],
      "text/plain": [
       "           2\n",
       "z⋅(x + 2⋅y) "
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'z \\\\left(x + 2 y\\\\right)^{2}'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(factor(x**2*z + 4*x*y*z + 4*y**2*z))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于多项式，`factor()`与`expand()`相反。 `factor()`对有理数使用完整的多元因式分解算法，这意味着由`factor()`返回的每个因子都保证是不可约的。如果您对因子本身感兴趣，factor_list将返回结构更清晰的输出。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASQAAAAXCAYAAACiYDJbAAAABHNCSVQICAgIfAhkiAAABm1JREFU\neJztnHuoFFUcxz/XF9oN1LIICZEyq3960DuSpjKFQIoefxSUW4YUPcjsKQlbRGb5R6HYC2qLkgzN\nzIKyh2veSw8qBakbVLA9zZTMIq2sbn/8znB3Z+c8ZmZnZ6bmA8vunXN+Z37f+f327DlnzlwoKSkp\nyTnPAD8CvVk7EgMPGGx6fWaoW2SdrpyAXIc5WTti4P8Qh/8StpyaQOt3cDCifQsnAv8ANweOXwws\nBTYBv6gGn3VpsMt4iG91oApcr6mn01kEosZiDbAN2D991yJTlDgcCFyNXMsvgL3AbqAP+WINy861\nRMTVZcqp/ZDvXhVo0N4h2exbWA/8DIwJHN+iGv4VGCD/HVLVUk+nswhEjcXJqs6C9F2LTFHicA1y\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ro\nO6Q2++a7FIPIfogJwBRHh2zUyN9cWadzLLLAeibir8svcY386dPh33oO27ZQo/s6bPl2BrJL+Ftg\nBtJxLUSWGbq19yjIQnXuj5DpzE5Huz5k3esxZXOXoW6N7sciri5TTsWyD+5DWo3sypyJPNfSzAXq\nBbKLE2RhsqY+7wRuCdj4HV5eFlJ9wnSuAE5HbkEeRvgIaxGykc0nK31xYjEDmSqsDWkvKx26fDsW\nmTbvRvaobFPHVwEfIutA05Bn+brFbOAe5BpuQhZ+gzQIXxzvB+Yiz23Nw7wu0+1YJNFlyikXrPaj\nkFtw74eUVTHfpWiE2GxGHv4cH9PhOHjYHx0J6hyGPBdm0rc9pJ0s9EH0WIxFFg5f0rSXlY6wfJui\nju0CjgmxmY5ojLN5t0L8KVsV+526usZ2GkP7bWxbR7odiyrxdNlyyqdO+JTN1Z47VQNJF93GIb3f\nAwnbiYqHvUOC5Dqz0heHGxCt00LKstbRqXzLMy8j1/gkS72sYxEFU041Uye8Q3K1ZzTywNu6aP61\nMQv4naEpRbfwaO3ddf8PKanOrPRFZQzyJPcqTXnWOjqVb3nFX8he5lA361i4Yssp2/9D0toH15BA\nLsjlyBPlvcjdgTisI5s9TQ1a9w3pFuiS6sxKX1QmI0/T1zTlWevoVL7liUlIR3Q4cAVyl+02B7us\nY+HKZMw5tQfz3j2bfUlJSQeZi4wKdgEv0PrYSElJSUlJSUlJSUlJSUlJEfkX+0JaK9hgqkgAAAAA\nSUVORK5CYII=\n",
      "text/latex": [
       "$$\\left ( 1, \\quad \\left [ \\left ( z, \\quad 1\\right ), \\quad \\left ( x + 2 y, \\quad 2\\right )\\right ]\\right )$$"
      ],
      "text/plain": [
       "(1, [(z, 1), (x + 2⋅y, 2)])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor_list(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    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03k5LrC/i6xFXFtgKeS/Cn+Ngj\nxLbfJdOaNYiS1hbh9iS/HofXKfMZfsPVSXZpDf5xie+5ZhwcIbbfblxTB39PMjamyth4kXa+Esdz\nIrX14YVmnEsoaX4R7kyyH2ns0uzRJPerCLpastMSf7Z1LUS5JqzAuaK1Gq161Z4nDijESk8Rrd2m\nCp6TRTrbIXrlLYneIyZgMiZgbSNOtoipuEekkrWiKGexWfXi3osZopWcKb9+lHFRsvNio04OEitd\n1H1szcgMFW3Y+xW0MYm2HSdVsTMp6co74NVCSXM7Ya7a3dXbObIT9Pf9te6XRoiC3MpubxizhYNF\nhWpfx0siss+og/cmMR8TBtSjDIaJy6pVe9PoXkS5GC+pg3e4uJTrGVCPcnCeuLT6v/xTZ5S401om\n6t+n6jtgjhNp79iBcmx/wgwR/dvFnf/IYvYOOuiggw46aC/+AWi/JlvLmENgAAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$z \\left(x + 2 y\\right)^{2}$$"
      ],
      "text/plain": [
       "           2\n",
       "z⋅(x + 2⋅y) "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(x**2*z + 4*x*y*z + 4*y**2*z)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请注意，从严格意义上讲，`factor`和`expand`的输入不必是多项式。 它们将智能地分解或扩展任何形式的表达式（尽管请注意，如果输入不再是有理数的多项式，则这些因子可能不会不可约）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAATwAAAAYCAYAAABnT0GSAAAABHNCSVQICAgIfAhkiAAABvFJREFU\neJztnHuIFVUcxz8+Ms1irTQqLC5lor0sCkkwMJMK6UWF5F9NmSL2oIQKLcl/0jIfRYRRJlsQaRkG\nUpRQrmVgupuCpf+UrJKuumVW1Gau3v74neHOzp0z99y5Zx7bng9c7t3z/n3PY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xpIYwjyhFtnpUT/f2xoDr1L\n975oc5FItZ8X9b9WRNENfIe8yNyKnNDUy2XIfsYS9PfSHBVsaA69S/e+aHORcP3c4XA4HA6Hw+Fw\nOByOav4DeGgCLP3UjHEAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$\\sin^{2}{\\left (x \\right )} + 2 \\sin{\\left (x \\right )} \\cos{\\left (x \\right )} + \\cos^{2}{\\left (x \\right )}$$"
      ],
      "text/plain": [
       "   2                           2   \n",
       "sin (x) + 2⋅sin(x)⋅cos(x) + cos (x)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expand((cos(x) + sin(x))**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\sin^{2}{\\\\left (x \\\\right )} + 2 \\\\sin{\\\\left (x \\\\right )} \\\\cos{\\\\left (x \\\\right )} + \\\\cos^{2}{\\\\left (x \\\\right )}'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(expand((cos(x) + sin(x))**2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKgAAAAYCAYAAABugbbBAAAABHNCSVQICAgIfAhkiAAABXpJREFU\naIHtmm2IVUUYx3+7mrlZrIVGhcWhtkV7sygkoaBEKqQ3KqT9dkuSsBdKKNCS+pKVpRURG2Wx9SW1\nDQMhSgjXMjBfSrCXT8lutHvTLduK2szdbh+eOe7cc2bOnHPPnJbc84fL3jvPMzP/+c9zZp6Zs1Ci\nRIkSJYxYAewGfgMGgS3ARePKqEQJDR8BdyFBeTGwGfgROM1V8W3gEDCtgU4DoAZ0NVC3CD4hLkd4\nLfHC6PhAXl1dmmbV/GRgFLgpyekK4B9gecpGowjwG6B5+ejYDFQRISY6fOn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I5qq/3EnxsqPDf5KDrsb8ibCOeg9pgmh+XrkVo\n2rDm+rZaQ+7cZhDPfW6gPh0IET6ZruW/ESTxAXkd1g38AFyHCL4K4Rm9p9Nxpfq7zRvTdOhEUplV\nyIk+Cv0U/6b6+zj1ueQk5KqmGQm+EFnmJ0nXojT1pnkHMoD7IuVDyAmsGxFoLXJlUEPyG/1OLsDP\nCprEZ67iVCW+DYZb6dWWvt5BAuVsiz0JFfLfg44iV0KbgKeAVxENo5P3rOrrIHIdswZZ1WrAp9Sf\nqLPMD5h1LVLTPJrXYQoy0M8j5fciL/0PIE/jYeSd6aPAKRHfAH8BauLTpsp+AS4x1AkvhXcabK1I\nom67TXChQv5XnfORre4QspUOIFc4dxh870Ryxd+RFxpfA48RP+BkmR+I61qkpnk1j2GFInOZrwZz\nwiefB0heCSYSfOnq0tS75lORU+IWXw3mhC8+Lchq1Z2b0fEBH7q6NM2tuemtzgjwBfIPCXuQE9x4\nwhef85Hcbw32+9iJBB+6ujQtNS9RokSJEiVKmPAvjrAksQqAMNoAAAAASUVORK5CYII=\n",
      "text/latex": [
       "$$\\left(\\sin{\\left (x \\right )} + \\cos{\\left (x \\right )}\\right)^{2}$$"
      ],
      "text/plain": [
       "                 2\n",
       "(sin(x) + cos(x)) "
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "factor(cos(x)**2 + 2*cos(x)*sin(x) + sin(x)**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\\\left(\\\\sin{\\\\left (x \\\\right )} + \\\\cos{\\\\left (x \\\\right )}\\\\right)^{2}'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "latex(factor(cos(x)**2 + 2*cos(x)*sin(x) + sin(x)**2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### collect\n",
    "\n",
    "`collect()` 收集表达式中术语的通用幂。 例如:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAQYAAAAaCAYAAABVVvJoAAAABHNCSVQICAgIfAhkiAAABY5JREFU\neJztnG2MXUMYx3+729JopeqtNIjQUNIqQvrFJlISQURQCT7USBsvqUilxEuI9aF2N9RqWgTBXUHi\nJSVoUK0ollKxZWlDVBbR7ap0vVO2XR+euend03vOnZkz55x7mV9ysrtnZp75zzxzzpnzzJyFQCAQ\n8MwC4FPgF328D5xbqCIzbgHWI5q3AS8D0wtVVB+Efgl44XzgbGAqcAywGPgHOKFIUQa8DlyBDPoZ\nwAvAVmD/IkXVAaFfApmxHbiqaBGWTAB2AucVLaTOCP3yP6XZo60W4BJkML3n0W4e7Iv0xVDRQhI4\nAJiPPMW/Av4EfgbeBebh15dlGqFfAuZ0AmuA75Dxsx3oBe5AxpdXZgC/AcPATzRGjCHKs0gHtRQt\nJIGrgRFgC/AU0A48hvT5CPA80OS5zkbol4A5fwPrkHHTASxDYkojwPfA4T4r2wuJMZyCDNYfaayA\n1b3IxXZUDnUpxAmnO5SdjUzpozODQ4Bvtd2LUmiL4qNfFO7trQcUja0/yriY84uRdj4QV3CVznBh\n5HwTUNJpHTUqXw08aqLSMy7au4ABYFqC3R5dNu5Ya6FRkc1Au1XbXVYlLat+MUHh3l4b3Yv034ti\nbB0L7ADettSgSOcvH9dTHsxEtLyRlGEnsJHR08cluuDDBpW8CTzprtEZW+1LgUHguBp2bwDaqhzf\naLttFhoV2dwYbtR2u6qkZdUvJijc22uje5Y+91yMrVXIq67tapkinb98XE95cBuiZ0lSppLOpPTf\n5afRM+w5je0AWoEjkVhDO7ALWcIsghJm2u9H1upnI1Px8jHBsJ67td3HsQv6KfzfGMYAfdruWTF5\nSuTTL1EU6dpbwkz3WOAP5JUqysW6zFKH+hXp/VXC/HrKi/LDrgt4R+v5BDg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2} z + 2 x^{2} + x y + x - 3$$"
      ],
      "text/plain": [
       " 3    2        2              \n",
       "x  - x ⋅z + 2⋅x  + x⋅y + x - 3"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expr = x*y + x - 3 + 2*x**2 - z*x**2 + x**3\n",
    "expr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAASEAAAAaCAYAAAD41+rBAAAABHNCSVQICAgIfAhkiAAABxdJREFU\neJztnHlsFUUcxz8tICAmiBdC1BgCiAl44oGBWBFFNAbviH/IIooaj4hoFBJjNSKgRCXgnWgxSoQU\nUdGoBEUoDQhEKkSLB6RigFYQRFQEKs8/fvPsY7v37MHCfJKX1+7O8X37nZ2d+c28BwaDwZBj7gHW\nAH+o1zLgqkwVBWM8sBLRvBWYD/TNVJHBYIjEcGAY0BPoDUwE9gFnZCkqAJ8Bo5COpx8wD2gEjslS\nlMFgiIftwJ1ZiwjJUcC/wNUead4CfgU6paIoH5wLFIDRWQuJwKHqZ5490aYNcDOwFxld5IluiHED\nXc73B/YDD6amKBuOBW5HRoY/AbuBncBSpFGXO+SZB2xBOvK8kDc/bwCmAzVICKEAvO2R/mDwZArw\nOfAL0o62A6uBx5F2Fiv9gD+BZuB38hETsjMHuUBtXM4vQD5bx9QUZcNdSAPfDLwDTALeQD57AagG\nymx5zlfnJqQnU5u8+VmHXONdQD3+ndDB4MleYDnSfiYjnehKpWsTcHKclR2BxIT6I412G9GCvBYi\nsCIuYQF5Drnperic7408NV9LTZEeFtGv42BkSmof8ZwIbFTlXu+Qrx74GfdO3A+L9LzPwk8Lvc93\nCdALeQBU4N8Jgb4nunRwOT4R0f9S8YC9sS1QCa6zHS8DqtS5ybZze5Gh+ypk1akOGBtBtC5RtD8P\njEBuvg0u5d6mypgdl1AHahF9bq/FCdZdyhfISuF+2/FG4BX1d4VDv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MnzdgHyNAWRqUADeT0GYRXSjktRercrpqVmOE52R\n4X8zcGtMZVroNfjHVP5VBP9C7yiVJ+rStUV63gfx81WVZhfSYel+sdki/U5I1xMd+iCrzXbKadms\nWJz2/h8TOhOZDuxE9hVsUcerkcY4HFmKr0lEsh5Ja5+L7BIeimzKjJNZwEXIMmoPWq/OgQRb98Rc\nrxsjgSeR6UcNEpS200DrYOrlKs8HCWpzIor3QfysBcYgWzbG4h0LS4Nr1Atabu4BtPiwDVkKLyUr\nTwCuQLaYLEGmg78hgemLkXbeCNxRmqGnOrgD55/gGIL0XElu1oNoT4s0tB+h6vhKowwnypEnrdcq\nRFOEci2iP3UrffQUgC9teTojwU+dEadFet4H8XOQyruC8FsknLDQGwlV4u1Jgy19HJ7o0BfZi1eH\ndJDNyINiJfJZzE/mRGA8YrZOcPJQ5T7k2gzKWkgI/Pz8EBlFnJeaonjJoycGHzogmzLnZy3kIKMj\nslxbnbWQkHj5WQxGz0hVUXzkzpOsvmGbN5qR/Q3tkVjDPu/khw29kHjVM8hPY+QFu5/dkJXJkcjv\n3awDbiKfPufVE4PhsGYMMvrZgXzLu7t3coPBYDAYDAaDwWAwGAwGg8Ggw3/IFqI5roisRgAAAABJ\nRU5ErkJggg==\n",
      "text/latex": [
       "$$x^{3} + x^{2} \\left(- z + 2\\right) + x \\left(y + 1\\right) - 3$$"
      ],
      "text/plain": [
       " 3    2                         \n",
       "x  + x ⋅(-z + 2) + x⋅(y + 1) - 3"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collected_expr = collect(expr, x)\n",
    "collected_expr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`collect()`与`.coeff()`方法结合使用特别有用。 `expr.coeff(x, n)`给出expr中`x**n`的系数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEEAAAASCAYAAAAE7bMcAAAABHNCSVQICAgIfAhkiAAAAbJJREFU\nWIXt1r9r1WAUxvFPawuCg2KLODn4Fzg7lXZwkg511ogOiiCCg5PQuhTRoYJIp6qgnYQO6iT+Ql1E\nULjQLgoXh6KLKB1UrK1D3gvytklvcl9aKPcLIeEk5zkPJ+97ErpsWwZwGnP4iJ/4gdc4hd6ts7Z5\nnMEqFnEfk5jB9xB/gJ4tc1eRTG56qGLeMI5a+8b343PQHGsFUy+LN6FA0fEycb0inuEhVqL4F0yH\n66FWsC9x8Tk8WSd+EgfwPHG9OvwJ5+XNLHpNvgpuq77yMvW2QxF9aATNI4k0S+nBrVDwpnqDKJO2\nCdeD3uOyh5rK93R83CvQ2YG74ZmrbRqsWvtOm7otzoe8Bez9/0Y8Ez7hVwXhxXVi/ZjFMYxjok2t\nKeyJYocwKm9oM7r3oV2TOIcbmMcIvlXIrcxOPJJ3/GICvUzn2+FC0GhgX+eWytmFp/LP0tlEmpnO\nmnAp5L/HYBpLxeyW/yMs43hC3Uz9JlwOue9EMyAm1X/CLA7jLQ7KZ0HMJH4nqrcRJ3AFf/FKPhRj\nmqoP10J6saR8kn+tqZ2ptxLGN/Czihc1PXXpsp35BzONhKm1Hli+AAAAAElFTkSuQmCC\n",
      "text/latex": [
       "$$- z + 2$$"
      ],
      "text/plain": [
       "-z + 2"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "collected_expr.coeff(x, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### cancel\n",
    "\n",
    "`cancel()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
